home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 2 / Gold Medal Software Volume 2 (Gold Medal) (1994).iso / graphvew / gifbla20.arj / SOURCE / UFFILE.C < prev    next >
C/C++ Source or Header  |  1992-12-15  |  4KB  |  235 lines

  1.  
  2. /* uffile.c - Portable fast file routines, UNIX version. */
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #include "ubasic.h"
  8. #include "uffile.h"
  9.  
  10. FFILE *
  11. ff_open(fpath,mode)
  12. char *fpath; int mode;
  13. {
  14.     FFILE *ff;
  15.  
  16.     if ((ff=(FFILE *)basic_alloc(sizeof(FFILE))) == NULL)
  17.         return NULL;
  18.     if ((ff->f=fopen(fpath,(mode==FF_READ?"rb":"wb"))) == NULL) {
  19.         basic_free(ff);
  20.         return NULL;
  21.     }
  22.     ff->mode = mode;
  23.     ff->pos = ff->count = (mode==FF_READ?1:0);
  24.     return ff;
  25. }
  26.  
  27. static void
  28. ff___fillbuf(ff)
  29. FFILE *ff;
  30. {
  31.     ff->pos = 0;
  32.     ff->count = fread(ff->buf,1,FFILEBUFSIZE,ff->f);
  33.     if (ff->count == 0)
  34.         ff->pos = ff->count = 1;
  35. }
  36.  
  37. int
  38. ff___inbuf(ff)
  39. FFILE *ff;
  40. {
  41.     ff___fillbuf(ff);
  42.     return (ff->pos==ff->count ? EOF : ff->buf[ff->pos++]);
  43. }
  44.  
  45. int
  46. ff_ungetc(c,ff)
  47. int c; FFILE *ff;
  48. {
  49.     return ((c==EOF || ff->pos==0) ? EOF : (ff->buf[--ff->pos]=c));
  50. }
  51.  
  52. size_t
  53. ff_read(buf,n,ff)
  54. char *buf; size_t n; FFILE *ff;
  55. {
  56.     size_t offset;
  57.  
  58.     offset = 0;
  59.     while (n-offset > ff->count-ff->pos) {
  60.         if (ff->count-ff->pos > 0)
  61.             memcpy(buf+offset,ff->buf+ff->pos,ff->count-ff->pos);
  62.         offset += (ff->count-ff->pos);
  63.         ff___fillbuf(ff);
  64.         if (ff->pos == ff->count)
  65.             return offset;
  66.     }
  67.     if (n-offset > 0)
  68.         memcpy(buf+offset,ff->buf+ff->pos,n-offset);
  69.     ff->pos += (n-offset);
  70.     return n;
  71. }
  72.  
  73. static void
  74. ff___flushbuf(ff)
  75. FFILE *ff;
  76. {
  77.     size_t count,i;
  78.  
  79.     if (ff->count == 0)
  80.         return;
  81.     count = fwrite(ff->buf,1,ff->count,ff->f);
  82.     if (0<count && count<ff->count) {
  83.         for (i=0; i<ff->count-count; i++)
  84.             ff->buf[i] = ff->buf[count+i];
  85.     }
  86.     ff->count -= count;
  87. }
  88.  
  89. int
  90. ff___outbuf(c,ff)
  91. int c; FFILE *ff;
  92. {
  93.     ff___flushbuf(ff);
  94.     if (ff->count != 0)
  95.         return EOF;
  96.     else
  97.         return (ff->buf[ff->count++] = c);
  98. }
  99.  
  100. int
  101. ff_unputc(ff)
  102. FFILE *ff;
  103. {
  104.     return (ff->count==0 ? EOF : ff->buf[--ff->count]);
  105. }
  106.  
  107. size_t
  108. ff_write(buf,n,ff)
  109. char *buf; size_t n; FFILE *ff;
  110. {
  111.     size_t offset;
  112.  
  113.     offset = 0;
  114.     while (n-offset > FFILEBUFSIZE-ff->count) {
  115.         if (FFILEBUFSIZE-ff->count > 0)
  116.             memcpy(ff->buf+ff->count,buf+offset,FFILEBUFSIZE-ff->count);
  117.         offset += (FFILEBUFSIZE-ff->count);
  118.         ff->count = FFILEBUFSIZE;
  119.         ff___flushbuf(ff);
  120.         if (ff->count != 0)
  121.             return offset;
  122.     }
  123.     if (n-offset > 0)
  124.         memcpy(ff->buf+ff->count,buf+offset,n-offset);
  125.     ff->count += (n-offset);
  126.     return n;
  127. }
  128.  
  129. int
  130. ff_seek(ff,offset,whence)
  131. FFILE *ff; long offset; int whence;
  132. {
  133.     if (ff->mode == FF_READ)
  134.         ff->pos = ff->count = 1;
  135.     else {
  136.         ff___flushbuf(ff);
  137.         if (ff->count != 0)
  138.             return -1;
  139.     }
  140.     return fseek(ff->f,offset,whence);
  141. }
  142.  
  143. long
  144. ff_tell(ff)
  145. FFILE *ff;
  146. {
  147.     long fpos;
  148.  
  149.     fpos = ftell(ff->f);
  150.     if (fpos == -1L)
  151.         return fpos;
  152.     else if (ff->mode == FF_READ)
  153.         return fpos-(ff->count-ff->pos);
  154.     else
  155.         return fpos+ff->count;
  156. }
  157.  
  158. int
  159. ff_flush(ff)
  160. FFILE *ff;
  161. {
  162.     if (ff->mode == FF_READ)
  163.         ff->pos = ff->count = 1;
  164.     else {
  165.         ff___flushbuf(ff);
  166.         if (ff->count != 0)
  167.             return EOF;
  168.     }
  169.     return fflush(ff->f);
  170. }
  171.  
  172. int
  173. ff_close(ff)
  174. FFILE *ff;
  175. {
  176.     int res;
  177.     FILE *f;
  178.  
  179.     if (ff->mode == FF_READ)
  180.         res = 0;
  181.     else {
  182.         ff___flushbuf(ff);
  183.         res = (ff->count!=0 ? EOF : 0);
  184.     }
  185.     f = ff->f;
  186.     basic_free(ff);
  187.     if (fclose(f) == EOF)
  188.         return EOF;
  189.     else
  190.         return res;
  191. }
  192.  
  193. void
  194. ff_start1bit(ff)
  195. FFILE *ff;
  196. {
  197.     ff->onebitpos = 0;
  198. }
  199.  
  200. int
  201. ff___in1bit(ff)
  202. FFILE *ff;
  203. {
  204.     ff___fillbuf(ff);
  205.     if (ff->pos == ff->count)
  206.         return -1;
  207.     ff->onebitpos++;
  208.     return (ff->buf[ff->pos]&1);
  209. }
  210.  
  211. int
  212. ff___out1bit(ff,bit)
  213. FFILE *ff; int bit;
  214. {
  215.     ff___flushbuf(ff);
  216.     if (ff->count != 0)
  217.         return -1;
  218.     ff->onebitpos++;
  219.     return (ff->buf[ff->count] = (bit&1));
  220. }
  221.  
  222. int
  223. ff_end1bit(ff)
  224. FFILE *ff;
  225. {
  226.     if (ff->onebitpos > 0) {
  227.         if (ff->mode == FF_READ)
  228.             ff->pos++;
  229.         else
  230.             ff->count++;
  231.         ff->onebitpos = 0;
  232.     }
  233.     return 0;
  234. }
  235.